package com.shaw.workrecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaw.workrecord.dto.Room;
import com.shaw.workrecord.dto.RoomMember;
import com.shaw.workrecord.enums.RoomMemberType;
import com.shaw.workrecord.exception.CustomerException;
import com.shaw.workrecord.mapper.RoomMapper;
import com.shaw.workrecord.mapper.RoomMemberMapper;
import com.shaw.workrecord.mapper.SystemUserMapper;
import com.shaw.workrecord.pojo.game.response.RoomMemberResponse;
import com.shaw.workrecord.service.IRoomMemberService;
import com.shaw.workrecord.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangjunhui
 * @description
 * @date 2022/9/15 14:29
 */
@Service
public class RoomMemberServiceImpl extends ServiceImpl<RoomMemberMapper, RoomMember> implements IRoomMemberService {

    @Autowired
    private SystemUserMapper userMapper;

    @Autowired
    private RoomMapper roomMapper;

    /**
     * 根据房间号查找玩家列表
     *
     * @param roomId 房间号
     * @return 玩家列表
     */
    @Override
    public List<RoomMemberResponse> getMemberList(Long roomId) {
        LambdaQueryWrapper<RoomMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.select().eq(RoomMember::getRoomId, roomId).orderByAsc(RoomMember::getSeat);
        List<RoomMember> list = baseMapper.selectList(wrapper);
        return list.stream().map(i -> {
            RoomMemberResponse roomMemberResponse = BeanUtil.copyProperties(i, RoomMemberResponse.class);
            roomMemberResponse.setNickName(userMapper.getNickName(roomMemberResponse.getMemberId()));
            return roomMemberResponse;
        }).collect(Collectors.toList());
    }

    /**
     * 加入房间
     *
     * @param roomId 房间号
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addMember(Long roomId) {
        Room room = roomMapper.getRoomById(roomId);
        if (room == null) {
            throw new CustomerException("房间不存在");
        }
        LambdaQueryWrapper<RoomMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.select().eq(RoomMember::getRoomId, roomId).orderByDesc(RoomMember::getSeat).last("limit 1");
        RoomMember member = baseMapper.selectOne(wrapper);
        RoomMember roomMember = new RoomMember();
        roomMember.setRoomId(roomId);
        roomMember.setMemberId(JWTUtils.getUserId());
        roomMember.setType(RoomMemberType.Other.getType());
        if (member == null) {
            roomMember.setSeat(2);
        } else {
            roomMember.setSeat(member.getSeat() + 1);
        }
        return null;
    }

    /**
     * 退出房间
     *
     * @param roomId 房间号
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeMember(Long roomId) {
        LambdaQueryWrapper<RoomMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.select().eq(RoomMember::getRoomId, roomId).eq(RoomMember::getMemberId, JWTUtils.getUserId());
        int result = baseMapper.delete(wrapper);
        // 如果房间下没有人员 则同时删除房间
        LambdaQueryWrapper<RoomMember> wrapper2 = new LambdaQueryWrapper<>();
        wrapper.select().eq(RoomMember::getRoomId, roomId);
        List<RoomMember> roomMembers = baseMapper.selectList(wrapper);
        if (roomMembers != null && roomMembers.size() == 0) {
            roomMapper.deleteById(roomId);
        }
        return result > 0;
    }
}
